package de.baumann.browser.model;

import java.util.List;

import de.baumann.browser.api.net.NodeApiService;
import de.baumann.browser.api.net.base.ApiBuilder;
import de.baumann.browser.api.net.vo.JoinApp;
import de.baumann.browser.api.net.vo.Member;
import de.baumann.browser.api.net.vo.Node;
import de.baumann.browser.api.net.vo.NodeDetail;
import de.baumann.browser.api.net.vo.NodeStatus;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.api.net.vo.Role;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.SPUtilKt;
import io.reactivex.Observable;
import io.reactivex.functions.Function;

public class NodeModel extends BaseModel {

    private NodeApiService nodeApiService;

    public NodeModel() {
        this.nodeApiService = ApiBuilder.buildService(NodeApiService.class);
    }

    public Observable<Result<Role>> queryNodeRole() {
        return nodeApiService.queryRole().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<Node>>> getNodeList() {
        return nodeApiService.getNodeList().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Node>> createNode(String nodeName, String nodeDesc) {
        return nodeApiService.createNode( nodeName, nodeDesc).compose(RxSchedulers.ioMain());
    }

    public Observable<Result> joinNode(String nodeId) {
        return nodeApiService.joinNode(nodeId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<NodeStatus>> nodeStatus(String nodeId) {
        SPUtilKt.put("isRefreshNode", false);
        return nodeApiService.nodeStatus(nodeId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<Member>>> members(String nodeId) {
        return nodeApiService.members(nodeId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<JoinApp>>> requestJoinList(String nodeId) {
        return nodeApiService.requestJoinList(nodeId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Member>> joinVerify(String applyId) {
        return nodeApiService.joinVerify(applyId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Object>> joinVerifyAction(String applyId, int status) {
        return nodeApiService.joinVerifyAction(applyId, status)
                .compose(RxSchedulers.ioMain())
                .map(objectResult -> {
                    SPUtilKt.put("isVerifyAction", true);
                    return objectResult;
                });
    }

    public Observable<Result<Object>> updateNodeDesc(String nodeId, String nodeDesc) {
        return nodeApiService.updateNodeDesc(nodeId, nodeDesc)
                .compose(RxSchedulers.ioMain());
    }

    public Observable<Result<NodeDetail>> nodeDetail(String nodeId) {
        return nodeApiService.nodeDetail(nodeId)
                .compose(RxSchedulers.ioMain());
    }

}
